cmake_minimum_required (VERSION 3.10)

project(example_grpc)

include(FindProtobuf)
find_package(Protobuf QUIET)

find_package(gRPC CONFIG QUIET)
if((gRPC_FOUND OR gRPC_DIR) AND Protobuf_FOUND)
    message(STATUS "build ${PROJECT_NAME}")

    add_library(grpc_ProtoLib proto/MessageDemo.proto)
    target_link_libraries(grpc_ProtoLib gRPC::grpc++)

    protobuf_generate(
        TARGET grpc_ProtoLib
        LANGUAGE cpp)

    protobuf_generate(
        TARGET grpc_ProtoLib
        LANGUAGE grpc
        PLUGIN protoc-gen-grpc=$<TARGET_FILE:gRPC::grpc_cpp_plugin>
        PLUGIN_OPTIONS generate_mock_code=true
        GENERATE_EXTENSIONS .grpc.pb.cc .grpc.pb.h)
    include_directories(${CMAKE_CURRENT_BINARY_DIR})

    aux_source_directory(. DIR_SRCS)
    add_executable(${PROJECT_NAME} ${DIR_SRCS})

    target_link_libraries(${PROJECT_NAME} Threads::Threads)
    target_link_libraries(${PROJECT_NAME} gRPC::grpc++)
    target_link_libraries(${PROJECT_NAME} gRPC::grpc++_reflection)
    target_link_libraries(${PROJECT_NAME} ${Protobuf_LIBRARIES})
    target_link_libraries(${PROJECT_NAME} grpc_ProtoLib)
endif()